using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Christian.Helle.PocketNews.PocketPC.Controls;
using Christian.Helle.PocketNews.PocketPC.NewsProtocol;
using System.Globalization;
using Christian.Helle.PocketNews.PocketPC.Data;

namespace Christian.Helle.PocketNews.PocketPC
{
	public partial class frmCompose : FormEx
	{
		private readonly Guid uniqueId;
		private Timer autoSave;

		public frmCompose()
		{
			InitializeComponent();

			uniqueId = Guid.NewGuid();

			lblNewsgroupValue.Text = Configuration.CurrentNewsgroup;
			lblSubjectValue.Text = string.Empty;

			tbMessage.Text = string.Format(
				"\r\n{0}",
				Configuration.Signature);

			tbMessage.SelectionStart = 0;
		}

		public frmCompose(string newsgroup, string subject, string body)
		{
			InitializeComponent();

			lblNewsgroupValue.Text = newsgroup;
			lblSubjectValue.Text = subject;

			tbMessage.Text = string.Format(
				"\r\n{0}\r\n\r\n ----- ORIGINAL MESSAGE ----- {1}", 
				Configuration.Signature,
				body);
			
			tbMessage.SelectionStart = 0;
		}

		public frmCompose(Guid guid, string newsgroup, string subject, string body)
			: this(newsgroup, subject, body)
		{
			uniqueId = guid;
		}

		private void Cancel_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void Send_Click(object sender, EventArgs e)
		{
		    var result = MessageBox.Show("Are you sure you want to send this message?", "Confirmation",
		                                 MessageBoxButtons.YesNo, 
                                         MessageBoxIcon.Question, 
                                         MessageBoxDefaultButton.Button2);
            if (result != DialogResult.Yes)
                return;

			ShowStatusBar();

			bool success = false;

			using (new WaitCursor()) {
				try {
					using (var nntp = new Nntp()) {
						UpdateStatusMethod("Connecting to news server...");
						nntp.Connect(Configuration.NntpServer);

						if (nntp.IsConnected) {
							nntp.SelectGroup(Configuration.CurrentNewsgroup);
							
							UpdateStatusMethod("Posting message...");
							nntp.PostMessage(
								lblSubjectValue.Text,
								tbMessage.Text,
								Configuration.Sender);
							
							UpdateStatusMethod("Successful");

							success = true;
						}
					}					
				}
				catch {
					UpdateStatusMethod("Unable to post message");
				}
			}

			if (success) {
				DialogResult = DialogResult.OK;
			}
		}

		private void ShowStatusBar()
		{
			if (!sbStatus.Visible) {
				sbStatus.Visible = true;
				sbStatus.Update();

				tbMessage.Height -= sbStatus.Height; 
			}
		}

		private void UpdateStatusMethod(string text)
		{
			sbStatus.Text = text;
			sbStatus.Update();
		}

		private void menuSave_Click(object sender, EventArgs e)
		{
			DataAccess.Instance.SaveDraft(
				uniqueId,
				lblNewsgroupValue.Text,
				lblSubjectValue.Text,
				tbMessage.Text);
		}

		private void frmCompose_Closing(object sender, CancelEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
				return;

			DialogResult result = MessageBox.Show(
				"Do you want to save this message in the Drafts folder?",
				"Save Message",
				MessageBoxButtons.YesNoCancel,
				MessageBoxIcon.Question,
				MessageBoxDefaultButton.Button1);

			if (result == DialogResult.Cancel)
				e.Cancel = true;

			if (result == DialogResult.Yes)
			{
				using (var wait = new WaitCursor())
				{
					if (Cache.Drafts != null)
					{
						foreach (var item in Cache.Drafts)
						{
							if (item.UniqueId == uniqueId)
							{
								DataAccess.Instance.UpdateDraft(
									uniqueId,
									lblNewsgroupValue.Text,
									lblSubjectValue.Text,
									tbMessage.Text);

								return;
							}
						}
					}

					DataAccess.Instance.SaveDraft(
						uniqueId,
						lblNewsgroupValue.Text,
						lblSubjectValue.Text,
						tbMessage.Text); 
				}
			}
		}
	}
}